﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Diagnostics;
using KNXFramework;
using KNXFramework.Settings;
using KNXFramework.Logging;
using KNXFramework.KNXObjects;


namespace WcfServiceLibrary1
{
    // NOTE: If you change the class name "ObjectService" here, you must also update the reference to "ObjectService" in App.config.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class ObjectService : IObjectService
    {
        private KNXConnection m_KNXConnection;
        private ObjectController m_ObjectController;
        private IMyCallback m_CallbackInstance;
        
        public ObjectService()
        {

        }

        #region IObjectService Members

        public bool StartConnection()
        {            
            if (m_KNXConnection == null)
            {                
                m_KNXConnection = new KNXConnection("localhost", 6720);
                m_KNXConnection.Start();                
                m_ObjectController = new ObjectController(m_KNXConnection);
                m_ObjectController.OnKNXObjectUpdate += new ObjectController.KNXObjectUpdateEventHandler(m_ObjectController_OnKNXObjectUpdate);
                m_CallbackInstance = OperationContext.Current.GetCallbackChannel<IMyCallback>();
                
            }            
            return true;
        }

        void m_ObjectController_OnKNXObjectUpdate(string id, string newValue)
        {
            m_CallbackInstance.UpdateKNXObjectValue(id, newValue);
        }


        public void CloseConnection()
        {            
            if (m_KNXConnection != null)
                m_KNXConnection.CloseConnection();
        }

        public KNXObject ReadValue(string id)
        {
            return new KNXObject(m_ObjectController[id]);
        }

        public void WriteValue(string id, string value)
        {
            m_ObjectController[id].SetObjectValue(value);
        }

        public Dictionary<string, KNXObject> GetAllObjects()
        {
            Dictionary<string, KNXObject> outputList = new Dictionary<string, KNXObject>(m_ObjectController.Count);
            foreach (KNXObjectBase baseObj in m_ObjectController.Values)
            {
                outputList.Add(baseObj.ID,new KNXObject(baseObj));
            }
            return outputList;
        }


        #endregion
    }
    [DataContract]
    public class KNXObject
    {
        string m_Id;

        [DataMember]
        public string Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }
        string m_ObjectValue;

        [DataMember]
        public string ObjectValue
        {
            get { return m_ObjectValue; }
            set { m_ObjectValue = value; }
        }
        public KNXObject(KNXObjectBase objBase)
        {
            Id = objBase.ID;
            ObjectValue = objBase.GetValue();
        }
    }
}
